<template>
	<view>
		<view class="header">
			<view id="back">
				<u-icon name="arrow-left" @click="clickBack" size="60rpx" v-if="back"></u-icon>
			</view>
			<view id="title">
				<text>登录</text>
			</view>
		</view>
		<view class="body">
			<view class="form" style="padding-top: 64rpx">
				<text style="color: #858597;">用户名</text>
				<u-input id="input" v-model="username" clearable fontSize="28rpx"></u-input>
			</view>
			<view class="form" style="padding-top: 48rpx">
				<text style="color: #858597;">密码</text>
				<u-input id="input" v-model="password" clearable fontSize="28rpx" password></u-input>
			</view>
			<view class="line" style="color: #858597; font-size: 28rpx;">
				<view>
					<text style="color: #858597;">还没有帐号?</text>
					<text @click="clickRegister" style="color: #3d5cff;">点击注册</text>
				</view>
				<view>
					<text @click="clickEditUrl" style="float: right;">后端地址</text>
				</view>
			</view>
			<view>
				<view class="btns">
					<u-button @click="clickLogin" class="button" text="登录"></u-button>
					<u-button @click="clickFaceLogin" class="button" text="刷脸登录"></u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var _this = null;
	const api = require('api.js');
	const message = require('message.js');
	const crypt = require('crypt.js');
	export default {
		data() {
			return {
				back: false,
				username: '',
				password: ''
			}
		},
		methods: {
			clickBack: function(e) {
				var pages = getCurrentPages();
				if (pages.length > 1) {
					uni.navigateBack({
						url: pages[pages.length - 2].url
					})
				}
			},
			clickRegister: function(e) {
				uni.preloadPage({
					url: "../register/register"
				});
				uni.redirectTo({
					url: '../register/register'
				});
			},
			clickEditUrl: function(e) {
				uni.navigateTo({
					url: 'editurl'
				})
			},
			clickLogin: function(e) {
				api.postSync('/api/user/login', {
					'username': _this.username,
					'password': crypt.encrypt(_this.password),
					'platform': 1
				}, (res) => {
					if (res.code != 200) {
						uni.showToast({
							icon: 'error',
							title: res.msg,
							duration: 2000
						})
					} else {
						uni.showToast({
							icon: 'success',
							title: "登录成功",
							duration: 2000
						});
						api.updateToken(res.data.token);
						api.connectWebSocket();
						message.loadMessageList();
						setTimeout(() => {
							uni.preloadPage({
								url: "../index/index"
							});
							uni.redirectTo({
								url: '../index/index'
							});
						}, 2000);
					}
				}, (err) => {
					uni.showToast({
						icon: 'error',
						title: "网络异常",
						duration: 2000
					})
				});
			},
			clickFaceLogin: function(e) {
				uni.navigateTo({
					url: '../face/facelogin'
				})
			}
		},
		onLoad() {
			_this = this;
			_this.back = getCurrentPages().length > 1;
		},
		onShow() {
			uni.hideToast();
		}
	}
</script>

<style lang="scss" scoped>
	.header {
		height: calc(var(--status-bar-height) + 318rpx);
		position: relative;
		background-color: #f0f0f2;

		#back {
			position: absolute;
			padding-top: calc(var(--status-bar-height) + 18rpx);
			padding-left: 12rpx;
			z-index: 99;
		}

		#title {
			position: absolute;
			padding-left: 48rpx;
			padding-top: calc(var(--status-bar-height) + 156rpx);
			color: #1f1f39;
			font-size: 64rpx;
			font-weight: 600;
		}
	}

	.body {
		padding-left: 48rpx;
		padding-right: 48rpx;
		background-color: white;
		border-top-left-radius: 30rpx;
		border-top-right-radius: 30rpx;
	}

	.form {
		color: rgba(31, 31, 57, 1);
		font-size: 28rpx;

		#input {
			margin-top: 6rpx;
			margin-bottom: 6rpx;
			height: 80rpx;
			border: 0.5px solid rgba(184, 184, 210, 1);
			border-radius: 25rpx;
		}
	}

	.line {
		padding-left: 12rpx;
		padding-right: 12rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		height: 40rpx;
		margin-top: 18px;
		margin-bottom: 24rpx;
	}

	.btns {
		display: flex;
		flex-direction: row;
	}

	.button {
		margin: 0 12rpx 0;
		width: 300rpx;
		height: 100rpx;
		border-width: 0;
		border-radius: 20rpx;
		background-color: $primary-color;
		color: white;
	}

	.signup {
		padding: 6rpx 24rpx 6rpx;
		font-size: 24rpx;
	}
</style>
